<?php
// ajouter ou modifier un événement
require_once('lib/event.php');
?>
<script src="javascript/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script src="javascript/tiny_mce/init.js" type="text/javascript"></script>
<?php
// champs des formulaire
$name = '';
$description = '';
$event_type = '';
$date_day = '';
$date_month = '';
$date_year = date('Y');

if (isset($_GET['id'])) {
	?>
	<h3>Modifier un événement</h3>
	<?php
	$data = event_get_by_id($_GET['id']);
	if ($data != NULL) {
		$name = $data['name'];
		$description = $data['description'];
		$event_type = $data['type'];
		$date_day = date('j', $data['time_event']);
		$date_month = date('n', $data['time_event']);
		$date_year = date('Y', $data['time_event']);
		
	}
	else {
		echo '<p class="error">Cet événement n\'existe pas</p>';
	}
}
else {
	?>
	<h3>Ajouter un nouvel événement</h3>
	<?php
}

?>
<p class="back">
	<a href="?p=club&amp;r=events">Revenir à la liste des événements</a>
</p>
<?php

if (isset($_POST['submit-event'])) {
	$name = clean_str($_POST['name']);
	$description = clean_str_preserve_tags($_POST['description']);
	$event_type = clean_str($_POST['event-type']);
	$date_day = intval($_POST['date-day']);
	$date_month = intval($_POST['date-month']);
	$date_year = intval($_POST['date-year']);

	$errors = array();
	if ($name == '') {
		$errors[] = 'Vous devez indiquer le nom de l\'événement';
	}
	if ($description == '') {
		$errors[] = 'Vous devez indiquer une description';
	}
	if ($event_type != 'C' && $event_type != 'S' && $event_type != 'E') {
		$errors[] = 'Le type est invalide';
	}
	if ($date_day == 0) {
		$errors[] = 'Le jour est invalide';
	}
	if ($date_month == 0) {
		$errors[] = 'Le mois est invalide';
	}
	if ($date_year == 0) {
		$errors[] = 'L\'année est invalide';
	}
	
	if (count($errors) == 0) {
		$timestamp = mktime(0, 0, 0, $date_month, $date_day, $date_year);
		if (isset($_GET['id'])) {
			// modification
			event_update($_GET['id'], $name, $description, $event_type, $timestamp);
			echo '<p class="success">L\'événement a été mis à jour</p>';
		}
		else {
			// ajout
			event_add($name, $description, $event_type, $timestamp);
			echo '<p class="success">Nouvel événement enregistré</p>';
		}
	}
	else {
		echo '<div class="error">Merci de corriger les erreurs suivantes :';
		print_array($errors);
		echo '</div>';
	}
}


?>
<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post">
	<p>
		<strong>Nom de l'événement :</strong><br />
		<input type="text" name="name" value="<?php echo $name ?>" size="50" />
	</p>
	<p>
		<strong>Description :</strong><br />
		<textarea name="description" rows="10" cols="50"><?php echo $description ?></textarea>
	</p>
	<p><strong>Type :</strong></p>
	<div style="margin-left: 40px;">
		<p>
			<input type="radio" name="event-type" value="C" id="type-C"
			<?php if ($event_type == 'C') echo ' checked="checked"' ?> />
			<label for="type-C"><?php echo event_type_name('C') ?></label>
		</p>
		<p>
			<input type="radio" name="event-type" value="S" id="type-S"
			<?php if ($event_type == 'S') echo ' checked="checked"' ?> />
			<label for="type-S"><?php echo event_type_name('S') ?></label>
		</p>
		<p>
			<input type="radio" name="event-type" value="E" id="type-E"
			<?php if ($event_type == 'E') echo ' checked="checked"' ?> />
			<label for="type-E"><?php echo event_type_name('E') ?></label>
		</p>
	</div>
	<p>
		<strong>Aura lieu le :</strong>
		Jour
		<select name="date-day">
		<?php
		for ($i = 1; $i <= 31; ++$i) {
			echo '<option value="'.$i.'"';
			if ($date_day == $i) {
				echo ' selected="selected"';
			}
			echo '>'.$i.'</option>';
		}
		?>
		</select>
		Mois
		<select name="date-month">
		<?php
		for ($i = 1; $i <= 12; ++$i) {
			echo '<option value="'.$i.'"';
			if ($date_month == $i) {
				echo ' selected="selected"';
			}
			echo '>'.$i.'</option>';
		}
		?>
		</select>
		Année
		<input type="text" name="date-year" class="js-numeric" maxlength="4" size="4" value="<?php echo $date_year ?>"/>
	</p>
	
	<p>
		<input type="submit" value="Envoyer" name="submit-event" />
	</p>
</form>

